from selenium import webdriver
import time,os
from selenium.webdriver.common.keys import Keys


class Agileone_opera():
    def __init__(self):
        os.system("teskkill /F /IM firefox.exe")
        self.driver = webdriver.Firefox() # 实例化浏览器对象
        self.driver.get("http://localhost/agileone") #发送访问Agileone的请求
        assert self.driver.find_element_by_id("login").is_displayed()==True   # 断言是否进入Agilone登录页面

        self.driver.find_element_by_id("username").send_keys("admin")  #输入用户名
        self.driver.find_element_by_id("password").send_keys("admin")  #输入密码
        self.driver.find_element_by_id("login").click()  #点击登录
        time.sleep(2)
        assert self.driver.find_element_by_xpath("//tr/td[2]/a[5]").text == '注销'  # 断言是否登录成功
        time.sleep(2)

    def upload(self):
        self.driver.find_element_by_partial_link_text("缺陷跟踪").click()  #点击缺陷跟踪
        time.sleep(2)
        self.driver.find_element_by_css_selector('#dtrow_2 > td:first-child > label').click()       # 点击编号，进入对应用例
        # dtrow_2 > td:nth-child(1) > label
        time.sleep(2)
        self.driver.find_element_by_id("showAttach").click()        #点击附件管理
        time.sleep(2)
        self.driver.find_element_by_id("fileToUpload").send_keys(r"D:\20181215000546.png")      # 上传图片
        time.sleep(2)
        self.driver.find_element_by_id("buttonUpload").click()    #点击上传

    def paste(self):
        self.driver.find_element_by_partial_link_text("缺陷跟踪").click()  # 点击缺陷跟踪
        time.sleep(2)
        str = 'test1test2'
        self.driver.find_element_by_css_selector("img.ke-common-icon.ke-icon-plainpaste").click() #点击粘贴按钮
        iframe = self.driver.find_element_by_xpath('//body/div[2]/div/div[2]/iframe')
        self.driver.switch_to.frame(iframe)  # 切换到内嵌页面粘贴窗口
        self.driver.find_element_by_id("textArea").send_keys('copy_1') # 
        self.driver.find_element_by_id("textArea").send_keys(Keys.CONTROL,'v')    # 粘贴内容
        # self.driver.switch_to.parent_frame()    # 切换到上层页面
        self.driver.switch_to.default_content()  # 切回主档
        self.driver.find_element_by_xpath('//input[@class="ke-dialog-yes"]').click()     #点击确定

    def change_win(self):
        self.driver

if __name__ == '__main__':
    op=Agileone_opera()
    op.paste()